<template>
  <div class="namelist-box">
    <div style="margin-bottom: 16px;border-bottom: 1px solid #eee;padding-bottom: 16px;">
      <div>
        产品名称:
        <a-input v-model:value="pgData.name" style="width: 300px;margin:0 20px;" placeholder="请输入产品名称" />
        产品分类:
        <a-tree-select v-model:value="pgData.type" allow-clear :tree-data="categorieList" style="width: 350px;"
          placeholder="请选择产品分类" tree-node-filter-prop="label">
        </a-tree-select>
        <a-button type="primary" style="margin-left: 20px;" @click="search">
          搜索
        </a-button>
      </div>
    </div>
    <div style="margin-bottom: 16px">
      <a-button type="primary" @click="openPop('add')">
        添加产品
      </a-button>
    </div>
    <a-table :columns="columns" :data-source="data" :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'img'">
          <a-image :src="record.img" class="img-box" width="80px" height="80px" />
        </template>
        <template v-if="column.key === 'desc'">
          {{ getDesc(record.desc) }}
        </template>
        <template v-if="column.key === 'type'">
          {{ findObjItem(categorieList, 'key', record.type).name }}
        </template>
        <template v-if="column.key === 'action'">
          <span class="table-operation">
            <a-button type="link" @click="openPop('change', record.id)">修改</a-button>
            <a-button type="link" danger @click="delProduct(record.id)">删除</a-button>
          </span>
        </template>
      </template>
    </a-table>
    <a-pagination class="pagination" v-model:current="pgData.page" :total="pgData.total" :pageSize="pgData.per_page"
      @change="pageChange" show-less-items />
    <productEdit :open="showAction" :editId="p_id" :categorieList="categorieList" @close="closePop">
    </productEdit>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from "vue";
import * as Api from "@req/api";
import { suc, getDesc, findObjItem } from "@/utils";
import { Modal, message } from "ant-design-vue";
import productEdit from "@/components/productEdit/index.vue";



const columns = [
  {
    title: "产品名称",
    dataIndex: "name",
    width: '250px'
  },
  {
    title: "产品封面",
    dataIndex: "img",
    key: 'img',
    width: '150px'
  },
  {
    title: "产品描述",
    dataIndex: "desc",
    key: "desc",
    ellipsis: true,
  },
  {
    title: "产品分类",
    dataIndex: "type",
    key: "type",
  },
  { title: "操作", dataIndex: "action", key: "action" },
];
const fileList = ref([]);
const data = ref([]);
const categorieList = ref([]);
const showAction = ref(false);
const p_id = ref(null);
const tableLoading = ref(false);


const pgData = ref({
  page: 1,
  per_page: 10,
  total: 0,
  name: null,
  type: null
})

const pageChange = () => {
  getProductList()
}


const handleCancel = () => {
  showAction.value = false;
}

const closePop = (type) => {
  showAction.value = false;
  p_id.value = null;
  type && getProductList()
}

onMounted(() => {
  getProductList()
  getCategoryList()
});

const search = () => [
  pgData.value.page = 1,
  getProductList()
]


const getProductList = () => {
  tableLoading.value = true;
  Api.getProductList(pgData.value).then((res) => {
    if (suc(res.status)) {
      pgData.value.total = res.data.pagination.total_products;
      data.value = res.data.data;
    }
    tableLoading.value = false;
  });
}


const getCategoryList = () => {
  Api.getCategoryList().then((res) => {
    if (suc(res.status)) {
      categorieList.value = handleList(res.data, 1);
    }
  });
}


const handleList = (list, level, pid) => {
  if (!list && list.length <= 0) return;
  for (const item of list) {
    if (pid) item.pid = pid;
    item.label = item.name;
    item.value = item.key;
    item.level = level;
    if (item.children.length <= 0) {
      delete item.children
    } else if (item.children) {
      item.children = handleList(item.children, level + 1, item.key);
    }
  }
  return list;
}

const openPop = (type, id) => {
  if (type != 'add') {
    p_id.value = id;
  } else {
    p_id.value = null;
  }
  showAction.value = true;
}

const delProduct = (id) => {
  Modal.confirm({
    title: "确认删除吗？",
    content: "删除后不可恢复，需重新添加",
    okText: "确认",
    cancelText: "取消",
    onOk() {
      Api.delProduct(id).then((res) => {
        if (suc(res.status)) {
          message.success(res.message)
          getProductList()
        }
      })
    },
  });
}

</script>

<style lang="postcss" scoped>
.namelist-box {
  padding: 16px;

  ::v-deep .ant-image {
    border-radius: 5px;
    overflow: hidden;

    .ant-image-img {
      object-fit: cover;
    }
  }

  .pagination {
    margin-top: 20px;
    text-align: right;
  }
}
</style>